package com.atguigu.jxc.controller;

import com.atguigu.jxc.dao.PurchaseDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.service.PurchaseService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zhmwi
 * @Date 2021/9/5
 * @Dersciption 报损单
 */
@RestController
@RequestMapping("damageListGoods")
public class DamageListGoodsController {

    @Autowired
    private DamageListGoodsService damageService ;

    /**
     * 保存报损单
     * @param damageList
     * @param damageListGoodsStr
     * @return
     */
    @PostMapping("save")
    public ServiceVO save(DamageList damageList, String damageListGoodsStr, HttpSession session){
        User currentUser = (User)session.getAttribute("currentUser");
        Integer userId = currentUser.getUserId();
        damageList.setUserId(userId);
        Gson gson = new Gson();
        List<DamageListGoods> damageGoodsList = gson.fromJson(damageListGoodsStr,
                new TypeToken<List<DamageListGoods>>(){}.getType());
        Boolean isSave = damageService.save(damageList,damageGoodsList);
        if (isSave){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(300,"请求失败");
    }

    /**
     * 报损单查询
     * @param sTime
     * @param eTime
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> list(String sTime ,String eTime){
        List<DamageList> damageList = damageService.list(sTime,eTime);
        Map result = new HashMap();
        result.put("rows",damageList);
        return result;
    }

    @PostMapping("goodsList")
    public Map<String,Object> goodsList(Integer damageListId){
        Map<String,Object> result = new HashMap<>();
        List<DamageListGoods> rows = damageService.getGoodsList(damageListId);
        result.put("rows",rows);
        return result;
    }

}
